package ms.jasim.framework;

import java.util.HashMap;
import java.util.Map;

public class ChartCreatorFactory {
	
	private static Map<String, Object> maps;

	static {
		registerChartCreator("Line Chart", LineChartCreator.class);
	}
	
	private static Map<String, Object> getMap() {
		return maps != null ? maps : (maps = new HashMap<String, Object>());
	}
	
	public static String[] getChartCreatorID() {
		return getMap().keySet().toArray(new String[0]);
	}
	
	public static IChartCreator getChartCreator(String chartCreatorID) {
		if (getMap().containsKey(chartCreatorID)) {
			Object res = getMap().get(chartCreatorID);
			if (res instanceof IChartCreator)
				return (IChartCreator) res;
			else if (res instanceof Class) {
				try {
					Object ins = ((Class) res).newInstance();
					if (ins instanceof IChartCreator) {
						getMap().put(chartCreatorID, ins);
						return (IChartCreator) ins;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	public static void registerChartCreator(String creatorID, Object instanceOrClazz) {
		getMap().put(creatorID, instanceOrClazz);
	}
}
